import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { MailerModule } from '@nestjs-modules/mailer';
import mailConfig from '../config/mail.config';
import { MailService } from './mail.service';

@Module({
  imports: [
    ConfigModule.forFeature(mailConfig),
    MailerModule.forRootAsync({
      imports: [ConfigModule],
      useFactory: async (configService: ConfigService) => {
        const mailConfig = configService.get('mail');
        return {
          transport: {
            host: mailConfig.host,
            port: mailConfig.port,
            secure: true,
            auth: {
              user: mailConfig.user,
              pass: mailConfig.password,
            },
            tls: { rejectUnauthorized: false },
          },
          defaults: {
            from: `"Resume Builder" <${mailConfig.from}>`,
          },
        };
      },
      inject: [ConfigService],
    }),
  ],
  providers: [MailService],
  exports: [MailService],
})
export class MailModule {} 